package com.test.kotlin.ui.widget

import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.widget.*
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SizeUtils
import com.test.kotlin.R

class TitleTabLayout @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) :HorizontalScrollView(context,attrs,defStyleAttr) {

    private var hGap:Int=SizeUtils.dp2px(10f)

    private  var tabContainer:RadioGroup

    private var contentMarginLeft=SizeUtils.dp2px(10f)
    private var contentMarginRight=SizeUtils.dp2px(10f)

    private var itemTextColor=0
    private var itemTextSize:Float=SizeUtils.dp2px(14f).toFloat()
    private var itemHeight=0

    private var selectedItem=0

    private var listener:OnTabItemSelectListener?=null

    init {
        attrs?.let {
            var typedArray=context.obtainStyledAttributes(it, R.styleable.TitleTabLayout)

            hGap=typedArray.getDimensionPixelOffset(R.styleable.TitleTabLayout_h_gap,hGap)

            contentMarginLeft=typedArray.getDimensionPixelOffset(R.styleable.TitleTabLayout_content_marginLeft,contentMarginLeft)
            contentMarginRight=typedArray.getDimensionPixelOffset(R.styleable.TitleTabLayout_content_marginRight,contentMarginRight)

            itemTextColor=typedArray.getResourceId(R.styleable.TitleTabLayout_item_textColor,0)
            itemTextSize=typedArray.getDimension(R.styleable.TitleTabLayout_item_textSize,itemTextSize)

            itemHeight=typedArray.getDimensionPixelOffset(R.styleable.TitleTabLayout_item_height,0)

            selectedItem=typedArray.getInt(R.styleable.TitleTabLayout_select_position,0)
            typedArray.recycle()
        }

        tabContainer= RadioGroup(context)
        tabContainer.orientation= LinearLayout.HORIZONTAL

        tabContainer.setPadding(contentMarginLeft,tabContainer.paddingTop,contentMarginRight,tabContainer.bottom)
        addView(tabContainer)
    }

    fun addTabItem(title:String){
        var rb = RadioButton(context).also {
            it.buttonDrawable = null
            it.text=title
            if(itemTextColor!=0) {
                it.setTextColor(resources.getColorStateList(itemTextColor))
            }
            it.setTextSize(TypedValue.COMPLEX_UNIT_PX,itemTextSize)
            var lp:RadioGroup.LayoutParams=RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,
            if(itemHeight==0) RadioGroup.LayoutParams.MATCH_PARENT else itemHeight)
            it.gravity=Gravity.CENTER
            if(tabContainer.childCount>0){
                lp.leftMargin=hGap
            }
            it.layoutParams=lp
            it.isChecked = tabContainer.childCount==selectedItem
            it.id=tabContainer.childCount
            it.tag=tabContainer.childCount
            it.setOnCheckedChangeListener(){ compoundButton: CompoundButton, b: Boolean ->
                LogUtils.i("cateoryName:${compoundButton.text} isChecked：$b，tag：${compoundButton.tag}")
                if(b&&compoundButton.tag is Int) {
                    selectedItem = compoundButton.tag as Int
                    listener?.onSelectedTabItem(compoundButton.text.toString(),selectedItem)
                }
            }
        }
        tabContainer.addView(rb)
    }

    fun setTabList(titles:List<String>){
        if(titles.isNotEmpty()){
            tabContainer.removeAllViews();
            titles.forEach { addTabItem(it) }
        }
    }

    fun setOnTabItemSelectListener(listener: OnTabItemSelectListener?){
        this.listener=listener
    }

    interface OnTabItemSelectListener{
        fun onSelectedTabItem(tabName:String,position:Int)
    }
}